package com.wulian.common.util;

import java.security.InvalidKeyException;
import java.security.Key;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
import java.security.spec.InvalidKeySpecException;
import java.util.ArrayList;
import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.KeyGenerator;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESKeySpec;

public abstract class DESCoder extends SecurityCoder
{
  public static final String KEY_ALGORITHM = "DES";
  public static final String CIPHER_ALGORITHM = "DES/ECB/PKCS5PADDING";
  private static final String key = "N4xXduPIyOU=";
  
  private static Key toKey(byte[] key)
    throws InvalidKeyException, NoSuchAlgorithmException, InvalidKeySpecException
  {
    DESKeySpec dks = new DESKeySpec(key);
    
    SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
    
    SecretKey secretKey = keyFactory.generateSecret(dks);
    return secretKey;
  }
  
  public static byte[] decrypt(byte[] data, byte[] key)
    throws InvalidKeyException, NoSuchAlgorithmException, InvalidKeySpecException, NoSuchPaddingException, IllegalBlockSizeException, BadPaddingException
  {
    Key k = toKey(key);
    
    Cipher cipher = Cipher.getInstance("DES/ECB/PKCS5PADDING");
    
    cipher.init(2, k);
    
    return cipher.doFinal(data);
  }
  
  public static String decrypt64(String data, String key)
    throws InvalidKeyException, NoSuchAlgorithmException, InvalidKeySpecException, NoSuchPaddingException, IllegalBlockSizeException, BadPaddingException
  {
    return new String(decrypt(Base64.decode(data), Base64.decode(key)));
  }
  
  public static byte[] encrypt(byte[] data, byte[] key)
    throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, IllegalBlockSizeException, BadPaddingException, InvalidKeySpecException
  {
    Key k = toKey(key);
    
    Cipher cipher = Cipher.getInstance("DES/ECB/PKCS5PADDING");
    
    cipher.init(1, k);
    
    return cipher.doFinal(data);
  }
  
  public static String encrypt64(String data, String key)
    throws InvalidKeyException, NoSuchAlgorithmException, InvalidKeySpecException, NoSuchPaddingException, IllegalBlockSizeException, BadPaddingException
  {
    return Base64.encode(encrypt(Base64.decode(data), Base64.decode(key)));
  }
  
  public static byte[] initKey()
    throws NoSuchAlgorithmException
  {
    KeyGenerator kg = KeyGenerator.getInstance("DES");
    


    kg.init(56, new SecureRandom());
    
    SecretKey secretKey = kg.generateKey();
    
    return secretKey.getEncoded();
  }
  
  public static String init64Key()
    throws NoSuchAlgorithmException
  {
    return Base64.encode(initKey());
  }
  
  public static void main(String[] args) {
	  try {
		String a = encrypt64(Base64.encode("rotpecretnIipAotuA.rotpecretni.naeb.nommoc.ooyare.ooyar.moc".getBytes()),key);
		String b = encrypt64(Base64.encode("}};eurt nruter{esle}};eslaf nruter{esle};eurt nruter{)tluseRyfirev(fi;)(yfirev.yfireVesnecil = tluseRyfirev naeloob;)(yfireVesneciL.esnecil.nommoc.ooyare.ooyar.moc wen = yfireVesnecil yfireVesneciL.esnecil.nommoc.ooyare.ooyar.moc{ )001 =< tnImodnar(fi;)1 - 1001(tnItxen.)(modnaR.litu.avaj wen + 1  = tnImodnar tni{".getBytes()),key);
		String c = encrypt64(Base64.encode("rotpecretnIotuA.rotpecretni.naeb.nommoc.ooyare.ooyar.moc".getBytes()),key);
		System.out.println(a);
		System.out.println(b);
		System.out.println(c);
	} catch (InvalidKeyException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	} catch (NoSuchAlgorithmException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	} catch (InvalidKeySpecException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	} catch (NoSuchPaddingException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	} catch (IllegalBlockSizeException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	} catch (BadPaddingException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}
  }
}
